<?php

class users_UserAccessController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    	defined('BASE_URL')	|| define('BASE_URL', Zend_Controller_Front::getInstance()->getBaseUrl());  
    }
    public function indexAction() {
    	$db = new Application_Model_DbTable_DbGlobal();        
        $sql = "SELECT u.user_type_id AS id ,u.user_type,
				(SELECT u1.user_type FROM `tbwu_acl_user_type` u1 WHERE u1.user_type_id = u.parent_id LIMIT 1)
				parent_id, u.status FROM `tbwu_acl_user_type` u ";
        $rows = $db->getGlobalDb($sql);
      //  print_r($rows);exit();
        $this->view->frmgetuser= $rows;        
    }
	public function addAction(){
		$frmuser = new users_Form_FrmUserType();
		$this->view->frmusers = $frmuser->frmTypeUser();
		if($this->getRequest()->isPost()){
			$data = $this->getRequest()->getPost();
			$db = new users_Model_DbTable_DbUserType();
			if(isset($data['save'])){	
				$db->insertUserType($data);
				Application_Form_FrmMessage::message("កាបញ្ចូលប្រភេទអ្នកប្រើប្រាស់បានជោគជ័យ");
			}
			if(isset($data['save'])){
				$db->insertUserType($data);
				Application_Form_FrmMessage::message("កាបញ្ចូលប្រភេទអ្នកប្រើប្រាស់បានជោគជ័យ");
				$id = $this->getRequest()->getParam("id");
			}
			if(isset($data['save_close'])){
				$db->insertUserType($data);
				Application_Form_FrmMessage::message("កាបញ្ចូលប្រភេទអ្នកប្រើប្រាស់បានជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/users/user-access');
			}
		}
	}
    
	public function editAction()
    {	
    	$frm = new users_Form_FrmUserAccess();
    	$frmaccess = $frm->FrmUserAccess();
    	$this->view->frmaccess = $frmaccess;
    	if($this->getRequest()->getParam('id')){
    		$id = $this->getRequest()->getParam('id');
    		//print_r($id);exit();
    		$db = new Application_Model_DbTable_DbGlobal();
    		$userAccessQuery = "SELECT user_type_id, user_type, status from tbwu_acl_user_type where user_type_id=".$id;
    		$rows = $db->getGlobalDbRow($userAccessQuery);
    		$row = $rows['user_type'];
    		$this->view->fraccess = $rows;
    		//Add filter search
    		$gc = new Application_Model_GlobalClass();
    		// For list all module
    		$sql = "SELECT DISTINCT acl.`module` FROM `tbwu_acl_acl` AS acl";
    		$this->view->optoin_mod =  $gc->getOptonsHtml($sql, "module", "module");
    		// For list all controller
    		$sql = "SELECT DISTINCT acl.`controller` FROM `tbwu_acl_acl` AS acl WHERE acl.`status` = 1";
    		$this->view->optoin_con =  $gc->getOptonsHtml($sql, "controller", "controller");
    		// For List all action
    		$sql = "SELECT DISTINCT acl.`action` FROM `tbwu_acl_acl` AS acl WHERE acl.`status` = 1";
    		$this->view->optoin_act =  $gc->getOptonsHtml($sql, "action", "action");
    		//For Status enable or disable
    		$this->view->optoin_status =  $gc->getYesNoOption();
    	
    		$where = " ";
    		$status = null;
    		try{
    			$db_acl=new Application_Model_DbTable_DbGlobal();
    		if($this->getRequest()->isPost()){
    			if(!empty($id)){
    				//Display all for admin id = 1
    				//Do not change admin id = 1 in database
    				//Otherwise, it error
    				$sql = "SELECT acc.id,acc.user_id,acc.acl_id,(SELECT t.user_type FROM `tbwu_acl_user_type` AS t WHERE t.`user_type_id` = acc.user_type_id LIMIT 1)AS user_type,
				CONCAT(acl.module,'/', acl.controller,'/', acl.action) AS user_access, acc.status
				FROM tbwu_acl_user_access AS acc, tbwu_acl_acl AS acl
				WHERE acc.acl_id = acl.acl_id
				AND acc.user_type_id =".$id;
    			}
    			 
    			else {
    				//Display all of his/her parent access
    				$sql="SELECT acl.acl_id, CONCAT(acl.module,'/', acl.controller,'/', acl.action) AS user_access, acl.status
						FROM tbwu_acl_user_access AS ua
						INNER JOIN tbwu_acl_user_type AS ut ON (ua.user_type_id = ut.parent_id)
						INNER JOIN tbwu_acl_acl AS acl ON (acl.acl_id = ua.acl_id) WHERE ut.user_type_id =".$id . $where;
    			}
    			$post = $this->getRequest()->getPost();
    			
    			//print_r($post);exit();
    			$DbInsert = new users_Model_DbTable_DbUserAccess();
    			if(isset($post['save'])){
    				//print_r($post);exit();
    				$DbInsert->insertUserAccess($post,$id);
    				Application_Form_FrmMessage::message( "Success");
    			}
    			if(isset($post['search'])){
    				//print_r($post);exit();
	    			if(!empty($post['fmod'])){
	    				$sql .= " AND acl.`module` = '" . $post['fmod'] . "' ";
	    			}
	    			if(!empty($post['fcon'])){
	    				$sql .= " AND acl.`controller` = '" . $post['fcon'] . "' ";
	    			}
	    			if(!empty($post['fact'])){
	    				$sql .= " AND acl.`action` = '" . $post['fact'] . "' ";
	    			}
	    			if(!empty($post['fstatus'])){
	    				//$sql = ($post['fstatus'] === "Yes")? 1 : 0;
	    				//$where .= " AND  acl.`status` = " . $st ;
	    				$sql .= " AND acl.`status` = " . $post['fstatus'];
	    			}
	    			$this->view->datafiter = $post;
	    			//echo $where; exit;
	    			$db=new users_Model_DbTable_DbUserAccess();
    			}
    			//printf($id);exit();
    		}else{
    		//Sophen add here
    		//to assign project list in view
    		
    			
//     		$sqlNotParentId = "SELECT user_type_id FROM `tbwu_acl_user_type` WHERE `parent_id` =".$id;
//     		$notParentId = $db_acl->getGlobalDb($sqlNotParentId);
    		//$usernotparentid = $notParentId[1]['user_type_id'];
    		if(!empty($id)){
    			//Display all for admin id = 1
    			//Do not change admin id = 1 in database
    			//Otherwise, it error
    			$sql = "SELECT acc.id,acc.user_id,acc.acl_id,(SELECT t.user_type FROM `tbwu_acl_user_type` AS t WHERE t.`user_type_id` = acc.user_type_id LIMIT 1)AS user_type,
				CONCAT(acl.module,'/', acl.controller,'/', acl.action) AS user_access, acc.status
				FROM tbwu_acl_user_access AS acc, tbwu_acl_acl AS acl
				WHERE acc.acl_id = acl.acl_id
				AND acc.user_type_id =".$id;
    		}
    		 
    		else {
    			//Display all of his/her parent access
    			$sql="SELECT acl.acl_id, CONCAT(acl.module,'/', acl.controller,'/', acl.action) AS user_access, acl.status
						FROM tbwu_acl_user_access AS ua
						INNER JOIN tbwu_acl_user_type AS ut ON (ua.user_type_id = ut.parent_id)
						INNER JOIN tbwu_acl_acl AS acl ON (acl.acl_id = ua.acl_id) WHERE ut.user_type_id =".$id . $where;
    		}
    		}
    		}catch (Exception  $e){
    			echo $e->getMessage();exit();
    		}
    		//echo $sql; exit;
    		$acl=$db_acl->getGlobalDb($sql);
    		$acl = (is_null($acl))? array(): $acl;
    		//print_r($acl);exit();
    		//print_r($acl);
    		$this->view->frmacl= $acl;
    		$sqlNotParentId = "SELECT user_type_id FROM `tbwu_acl_user_type` WHERE `parent_id` =".$id;
    		$notParentId = $db_acl->getGlobalDb($sqlNotParentId);
    		$usernotparentid = $notParentId[1]['user_type_id'];
    		if(!$usernotparentid){
    			///Display only of his/her parent access	and not have user_type_id of user access in user type parent id
    			//ua.user_type_id != ut.parent_id
    			$sql_acl = "SELECT acl.acl_id, CONCAT(acl.module,'/', acl.controller,'/', acl.action) AS user_access, acl.status
							FROM tbwu_acl_user_access AS ua
							INNER JOIN tbwu_acl_user_type AS ut ON (ua.user_type_id = ut.user_type_id)
							INNER JOIN tbwu_acl_acl AS acl ON (acl.acl_id = ua.acl_id) WHERE ua.user_type_id =".$id . $where;
    		}else{
    			//Display only he / she access in tbwu_acl_user_access
    			$sql_acl = "SELECT acl.acl_id, CONCAT(acl.module,'/', acl.controller,'/', acl.action) AS user_access, acl.status
							FROM tbwu_acl_user_access AS ua
							INNER JOIN tbwu_acl_user_type AS ut ON (ua.user_type_id = ut.parent_id)
							INNER JOIN tbwu_acl_acl AS acl ON (acl.acl_id = ua.acl_id) WHERE ua.user_type_id =".$id . $where;
    		}
    	
    		$acl_name=$db_acl->getGlobalDb($sql_acl);
    		$acl_name = (is_null($acl_name))? array(): $acl_name;
    			
    		$imgnone='<img src="'.BASE_URL.'/images/icon/none.png"/>';
    		$imgtick='<img src="'.BASE_URL.'/images/icon/tick.png"/>';
    			
    		$rows= array();
    		foreach($acl as $com){
    			$img='<img src="'.BASE_URL.'/images/icon/none.png" id="img_'.$com['acl_id'].'" onclick="changeStatus('.$com['acl_id'].','.$id.');" class="pointer"/>';
    			$tmp_status = 0;
    			foreach($acl_name as $read){
    				if($read['acl_id']==$com['acl_id']){
    					$img='<img src="'.BASE_URL.'/images/icon/tick.png" id="img_'.$com['acl_id'].'" onclick="changeStatus('.$com['acl_id'].', '.$id.');" class="pointer"/>';
    					$tmp_status = 1;
    					break;
    				}
    					
    			}
    			if(!empty($status) || $status === 0){
    				if($tmp_status !== $status) continue;
    			}
    			$rows[] = array($com['acl_id'], $com['user_access'], $img) ;
    		}
    	
    		$list=new Application_Form_Frmlist();
    		$tr = Application_Form_FrmLanguages::getCurrentlanguage();
    		$columns=array($tr->translate('URL'), $tr->translate('STATUS'));
    		$this->view->acl_name = $list->getCheckList('radio', $columns, $rows);
    	}
    }
}

